package dao.Impl;

import dao.DeptDao;
import org.apache.ibatis.session.SqlSession;
import pojo.Dept;
import util.MyBatisUtil;

import java.util.List;

public class DeptDaoImpl implements DeptDao {
    @Override
    public List<Dept> findAll(Dept dept) {
        try (SqlSession session = MyBatisUtil.getSession()) {
            return session.getMapper(DeptDao.class).findAll(dept);
        }
    }

    @Override
    public int addDept(Dept dept) {
        try (SqlSession session = MyBatisUtil.getSession()) {
            int result = session.getMapper(DeptDao.class).addDept(dept);
            session.commit();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Dept findById(Integer id) {
        try (SqlSession session = MyBatisUtil.getSession()) {
            return session.getMapper(DeptDao.class).findById(id);
        }
    }

    @Override
    public int updateDept(Dept dept) {
        try (SqlSession session = MyBatisUtil.getSession()) {
            int result = session.getMapper(DeptDao.class).updateDept(dept);
            session.commit();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteDeptById(Integer id) {
        try (SqlSession session = MyBatisUtil.getSession()) {
            int result = session.getMapper(DeptDao.class).deleteDeptById(id);
            session.commit();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
